package hdf5.gui.listeners;

import common.GifSequenceWriter;
import hdf5.gui.Player;

import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class SaveAsGifListener extends SaveAsListener{


    public SaveAsGifListener(Component component){
        setFileChooserProperty();
        this.component = component;
    }

    @Override
    protected String getExtension() {
        return "gif";
    }

    @Override
    protected void saveAs(File file) throws IOException {
        final int delay = 3;

        ImageOutputStream imageOut = new FileImageOutputStream(file);
        Player player = Player.getInstance();
        BufferedImage image = player.getImage();
        GifSequenceWriter writer = new GifSequenceWriter(imageOut, image.getType(), delay, true);
        int currentStep = player.getStep();
        player.setStep(0);
        do{
            writer.writeToSequence(player.getImage());
        } while(player.next());
        player.setStep(currentStep);
        writer.close();
    }
}
